#ifndef _packet_h
#define _packet_h


/** 
 * General packet
 * Length(1 byte); 
 * Destination address(1 byte); 
 * Source address(1 byte); 
 * Packet Type Identifier(1 byte) ;
    ? H: 'Host Beacon',
    ? R: 'Join Game Request'
    ? Z: 'Heartbeat'
    ? G: 'Guess',
    ? Y: 'Guess Response',
    ? S: 'Server Messages'
 * Packet body(variable length).
 * note: the semicolons are field separators, do not send ?;? in your packet.

  BEACON Format: 
  LEN(1);DST(1, broadcast address);SRC(1);'H'; 'Player name'(up to 20 bytes, null terminated string)

  CONNECTION RESQUEST Format:
  LEN(1);DST(1);SRC(1);'R';'Player name'(up to 20 bytes, null terminated string)

  Heartbeat packet format:
  LEN(1);DST(1);SRC(1);'Z';'<NUM>'(1 byte indicating how many heartbeat I sent since I last got an
  heartbeat)

  Guess attempt format:
  LEN(1);DST(1);SRC(1);'G';'<Guess Count>'(2 bytes);'<Guess value>'(4 character ASCII guess e.g.:
  '1234', 0-9)

  Guess Reply format:
  LEN(1);DST(1);SRC(1);'Y';'<B>'(1 byte, number of bulls, ASCII encoded);'<C>'(1 byte, number of
  cows, ASCII encoded)

  Server Message Format:
  LEN(1);DST(1,broadcast address); SRC(1);'S';'T'(game termination);'String message' ( Up to 30
  characters, NULL terminated )
 */

#define TYPE_SERVER 'S'
#define TYPE_SERVER_TERMINATE 'T'
#define TYPE_JOINGAME 'R'
#define TYPE_HOSTGAME 'H'
#define TYPE_HEARTBEAT 'Z'
#define TYPE_GUESS 'G'
#define TYPE_REPLY 'Y'

//general packet definition
typedef struct  {
    char length;
    char dest;
    char src;
    char type;
    char body[64];
}PACKET;

//packet for connection request
typedef struct {
  PACKET parent;
  char playerName[20];
}PACKET_CONNECTION;

typedef struct {
  PACKET parent;
  //type = TYPE_HEARTBEAT;
  char num;
}PACKET_HEARTBEAT;

//packet for guesses
typedef struct {
  PACKET parent;
  //type = TYPE_GUESS;
  int guessCount;
  char guessValue[4];  //ascii
}PACKET_GUESS;

//packet for reply of guess
typedef struct {
  PACKET parent;
  //type = TYPE_REPLY;
  char bulls; //ascii
  char cows; //ascii
}PACKET_REPLY;

//packet for server message
typedef struct {
  PACKET parent;
  //type = TYPE_SERVER;
  char serverOrder;
  char message[30];
}PACKET_SERVER;

/****************PROTOTYPES*******************/

//like a contructor method for initializing needed variables
void initPacket(char source, char destination, char username[20]);

/*************MUTATOR METHODS***************/
void setUsername(char username[20]);
void setDestAddr(char destination);
void setSrcAddr(char source);
/*************END MUTATOR METHODS***************/

/*************HELPER METHODS****************/
void sendPacket(PACKET *packet); //sending packets **********
void createGuessPacket(PACKET *packet, int guessCount, char guessValue[4]); // packet for guessing
void createReplyPacket(PACKET *packet, char bulls, char cows); //packet for reply
void createServerPacket(PACKET *packet, char serverExecution, char *msg); //packet server message also for termination
void createHeartbeatPacket(PACKET *packet, char num); //packet fpr heartbeat
void createHostbeaconPacket(PACKET *packet); //packet for hosting game
void createClientPacket(PACKET *packet); //packet for connection to server
PACKET receivePacket(char buffer[64], char len);
/*************END HELPER METHODS****************/

#endif

